using System;
using PrimeTween;
using UnityEngine;

namespace Bubbles
{
    public class TypingIndicator : MonoBehaviour
    {
        [SerializeField] private SpriteRenderer[] dots;
        
        private SpriteRenderer _renderer;
        
        private Color _defaultColor = Color.white;
        private Color _defaultTransparent = Color.white;
        
        private Color _dotColor = Color.white;
        private Color _dotTransparent = Color.white;
        
        
        private void Awake()
        {
            _renderer = GetComponent<SpriteRenderer>();

            _defaultColor = _renderer.color;
            _defaultTransparent = _renderer.color;
            _defaultTransparent.a = 0f;
            
            _dotColor = dots[0].color;
            _dotTransparent = dots[0].color;
            _dotTransparent.a = 0f;
        }

        public void SetVisibility(bool isVisible)
        {
            _renderer = GetComponent<SpriteRenderer>();

            _renderer.color = isVisible ? _defaultColor : _defaultTransparent;
            foreach (SpriteRenderer dot in dots)
            {
                dot.color = isVisible ? _dotColor : _dotTransparent;
            }
        }

        public void Show()
        {
            Tween.Alpha(_renderer, 1f, 0.3f);
            foreach (SpriteRenderer dot in dots)
            {
                Tween.Alpha(dot, 1f, 0.3f);
            }
        }
        
        public void Hide()
        {
            Tween.Alpha(_renderer, 0f, 0.3f);
            foreach (SpriteRenderer dot in dots)
            {
                Tween.Alpha(dot, 0f, 0.3f);
            }
        }
    }
}
